﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Invoicing.Model.Orders;
using Invoicing.Command.Orders;
using Utils;
namespace Invoicing.Logic.Orders
{
    public class DealOrderLogic
    {
        public static DealOrderModel NewOrder(DealOrderModel order)
        {
            if (order != null)
            {
                if (order.Items == null || order.Items.Count == 0)
                    throw new InvoicingException("没有内容");
                if (order.CustomerID == 0)
                    throw new InvoicingException("请选择一个客户");
                var customer = Members.CustomerLogic.GetModel(order.CustomerID);
                if (customer == null)
                    throw new InvoicingException("无效的客户");

                var sta = order.Items.GroupBy(p => new { p.ProductID, p.MaterialID, p.ProcessID, p.Size }).Select(p => new { p.Key, Ct = p.Count() });
                if (sta.Any(p => p.Ct > 1))
                    throw new InvoicingException("同一张单据中不充许有相同的产品记录");

                foreach (var it in order.Items)
                {
                    var product = Products.ProductLogic.GetModel(it.ProductID);
                    if (product == null)
                        throw new ApplicationException("无效的产品");
                    it.Product = product;
                    if (it.UnitPrice < 0 || it.Quantity <= 0)
                        throw new ApplicationException("无效的单价或数量（只)");
                    it.Money = it.UnitPrice * it.Quantity;
                    


                    //if (it.Product.PType == 1)
                    //{
                        
                    //}
                    //else
                    //{
                    //    if (it.Quantity < 0)
                    //        throw new ApplicationException("无效的数量（只)");
                    //    if (it.WeightQuantity < 0 || it.WeightUnitPrice<0)
                    //        throw new ApplicationException("无效的单价或数量（斤)");

                    //    it.Money = it.WeightUnitPrice * it.WeightQuantity;
                    //    it.UnitPrice = it.Money / it.Quantity;
                    //}
                    if (it.MaterialID==0)
                        throw new ApplicationException("无效的材料");
                    if (it.ProcessID==0)
                        throw new ApplicationException("无效的加工");
                    if (string.IsNullOrEmpty(it.Size))
                        throw new ApplicationException("无效的规格");
                }

                order.OrderStatus = (int)OrderStatus.New;
                order.CreatedOn = DateTime.Now;
                order.TotalMoney = order.Items.Sum(p => p.Money);
                return DealOrderCommand.NewOrder(order);
            }
            return null;
        }
        /// <summary>
        /// 取得最近一次交易的价格
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="SupplierID"></param>
        /// <returns></returns>
        public static decimal GetLastPrice(int productID, int customerID)
        {
            return DealOrderCommand.GetLastPrice(productID, customerID);
        }

        public static DealOrderCollection GetOrders(DealOrderQuery sea)
        {
            return DealOrderCommand.GetOrders(sea);
        }

        public static DealOrderModel GetOrder(string orderNo)
        {
            return DealOrderCommand.GetOrder(orderNo);
        }

        public static DealOrderModel GetOrderWithProductOrders(string orderNo)
        {
            var order = DealOrderCommand.GetOrderWithProductOrders(orderNo);
            order.Items.ForEach(p =>{
                decimal cou = 0;
                order.ProductOrders.ForEach(o => {
                    cou += o.Items.Where(r => r.ProductID == p.ProductID && r.MaterialID==p.MaterialID && r.ProcessID==p.ProcessID && r.Size==p.Size).Sum(r => r.Quantity);
                });
                p.Quantity -= cou;
            });
            return order;
        }

        public static bool CancelOrder(string orderNo)
        {
            return DealOrderCommand.CancelOrder(orderNo);
        }
    }
}
